<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar title="编辑资料" left-arrow @click-left="$router.back()" fixed />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image round class="avatar" :src="profileObj.photo" @click="imageClickFn"/>
      <!-- file 选择框 -->
        <input
          type="file"
          ref="iptFile"
          v-show="false"
          accept="image/*"
          @change="onFileChange"
          />
        </template>
      </van-cell>
      <van-cell title="昵称" is-link  :value="profileObj.name" @click="nameClickFn"/>
      <van-cell title="生日" is-link
      :value="profileObj.birthday" @click="birthdayClickFn"/>
    </van-cell-group>

    <!-- 姓名修改 -->
    <van-dialog v-model="show" title="修改昵称" show-cancel-button :before-close="beforeCloseFn">
      <input type="text" v-fofo v-model="inputUserName">
    </van-dialog>

    <!-- 时间年月日 -->
    <van-popup round v-model="dateTimeShow" position="bottom" :style="{ height: '50%' }">
        <van-datetime-picker
          v-model="currentDate"
          type="date"
          title="选择年月日"
          :min-date="minDate"
          :max-date="maxDate"
          @cancel="datetimePickerFn"
          @confirm="confirmFn"
        />
    </van-popup>
  </div>
</template>

<script>
import { userProfileAPI, updateUserPhotoAPI, updateUserProfileAPI } from '@/api/index'
import { Notify, Toast } from 'vant'
import { formatDate } from '@/utils/date'
export default {
  name: 'UserEdit',
  data () {
    return {
      profileObj: {}, // 用户基本资料
      show: false, // 控制姓名修改输入框显示/隐藏
      inputUserName: '',
      minDate: new Date(1930, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(2022, 5, 15),
      dateTimeShow: false
    }
  },
  async created () {
    const res = await userProfileAPI()
    console.log(res)
    this.profileObj = res.data.data
  },
  methods: { // 文件选择
    async onFileChange (e) {
      if (e.target.files.length === 0) return // 用户点取消
      console.log(e.target.files[0])
      // 创建FormData对象
      const theFd = new FormData()
      theFd.append('photo', e.target.files[0])

      const res = await updateUserPhotoAPI(theFd)
      this.profileObj.photo = res.data.data.photo
    },
    // 图片点击
    imageClickFn () {
      this.$refs.iptFile.click()
    },
    // 姓名点击
    nameClickFn () {
      this.show = true
      this.inputUserName = this.profileObj.name
    },
    
    // 姓名确认
    async beforeCloseFn (action, done) {
      if (action === 'confirm') {
        if (/^[A-Za-z0-9\u4e00-\u9fa5]{1,7}$/.test(this.inputUserName) === true) {
          await updateUserProfileAPI({
            name: this.inputUserName
          })
          done()
          this.profileObj.name = this.inputUserName
          Toast.success('修改昵称成功')
        } else {
          Notify({ type: 'warning', message: '用户名中英文和数字1-7位' })
          // 阻止弹窗关闭
          done(false)
        }
      } else {
        done()
      }
    },
    // 生日 点击事件
    birthdayClickFn () {
      this.dateTimeShow = true
      this.currentDate = new Date(this.profileObj.birthday)
    },
    // 日期取消 事件
    datetimePickerFn () {
      this.dateTimeShow = false
    },
    async confirmFn () {
      await updateUserProfileAPI({
        birthday: formatDate(this.currentDate)
      })
      this.dateTimeShow = false
      this.profileObj.birthday = formatDate(this.currentDate)
      Toast.success('修改生日成功')
    }
  }
}
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
::v-deep .van-dialog__content{
  text-align: center;
  input {
    margin-top: 20px;
    margin-bottom: 20px;
    padding: 0;
    outline: none;
    border: none;
    text-align: center;
  }
}
</style>
